package com.qf.boot2202day02.controller;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("uploader")
public class UploadController {

    @Value("${myfile.dir}")
    private String fileDir;


    @PostMapping("/test1")
    public String upload(MultipartFile file) throws IOException {

        String filename = UUID.randomUUID().toString();

        String originalFilename = file.getOriginalFilename();

        String extension = FilenameUtils.getExtension(originalFilename);

        String finalFilePath = fileDir + File.separator + filename+"."+extension;

        file.transferTo(new File(finalFilePath));

        return "ok ";

    }
}
